﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Configuration;

namespace CrawlServiceCore.Configuration
{
    public class CrawlSiteElementCollection : ConfigurationElementCollection
    {
        public CrawlSiteElementCollection()
        {
        }

        public int CollectionCount
        {
            get { return base.Count; }
        }

        #region Configuration Attributes / Properties
        [ConfigurationProperty("siteurl", IsRequired = true)]
        [SettingsDescription("The complete url of the site which the included lists reside.")]
        public string SiteUrl
        {
            get { return (string)base["siteurl"]; }
            set { base["siteurl"] = value; }
        }

        [ConfigurationProperty("sitename", IsRequired = true, IsKey = true)]
        [SettingsDescription("A unique name to reference this site by.")]
        public string SiteName
        {
            get { return (string)base["sitename"]; }
            set { base["sitename"] = value; }
        }
        #endregion

        #region Overrides
        protected override ConfigurationElement CreateNewElement()
        {
            return new CrawlListElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((CrawlListElement)element).ListName;
        }
        #endregion

        #region Children Management
        public CrawlListElement this[int index]
        {
            get { return (CrawlListElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);

                BaseAdd(index, value);
            }
        }

        new public CrawlListElement this[string ListName]
        {
            get { return (CrawlListElement)BaseGet(ListName); }
        }

        public int IndexOf(CrawlListElement element)
        {
            return BaseIndexOf(element);
        }

        public void Add(CrawlListElement element)
        {
            BaseAdd(element);
        }
        protected override void BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);
        }

        public void Remove(CrawlListElement element)
        {
            if (BaseIndexOf(element) >= 0)
                BaseRemove(element.ListName);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string ListName)
        {
            BaseRemove(ListName);
        }

        public void Clear()
        {
            BaseClear();
        }
        #endregion
    }
}
